//
// Created by XiongYingqi on 14-5-20.
// Copyright (c) 2014 XiongYingqi. All rights reserved.
//

#import "XYQDrawPersonImage.h"


@implementation XYQDrawPersonImage {

}

+ (UIColor *)randomColor {
    CGFloat r = arc4random() % 255;
    CGFloat g = arc4random() % 255;
    CGFloat b = arc4random() % 255;
    UIColor *color = [UIColor colorWithRed:r / 255 green:g / 255 blue:b / 255 alpha:1];
    return color;
}

- (UIImage *)addText:(UIImage *)img text:(NSString *)text1 {
    //get image width and height
    int w = img.size.width;
    int h = img.size.height;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    //create a graphic context with CGBitmapContextCreate
    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
    CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
    CGContextSetRGBFillColor(context, 0.0, 1.0, 1.0, 1);
    char *text = (char *) [text1 cStringUsingEncoding:NSASCIIStringEncoding];
    CGContextSelectFont(context, "Georgia", 30, kCGEncodingMacRoman);
    CGContextSetTextDrawingMode(context, kCGTextFill);
    CGContextSetRGBFillColor(context, 255, 0, 0, 1);
    CGContextShowTextAtPoint(context, w / 2 - strlen(text) * 5, h / 2, text, strlen(text));

    //Create image ref from the context
    CGImageRef imageMasked = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    return [UIImage imageWithCGImage:imageMasked];
}

- (UIImage *)getImage {
    CGSize imageSize = CGSizeMake(50, 50);
    UIGraphicsBeginImageContextWithOptions(imageSize, 0, [UIScreen mainScreen].scale);
    [[XYQDrawPersonImage randomColor] set];
    UIRectFill(CGRectMake(0, 0, imageSize.width, imageSize.height));
    UIImage *pressedColorImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return pressedColorImg;

}

UIImage *defaulImage;

+ (UIImage *)getDefaultImage {
    if (defaulImage == nil) {
        CGFloat width = 60;
        CGSize imageSize = CGSizeMake(width, width);
        UIGraphicsBeginImageContextWithOptions(imageSize, 0, [UIScreen mainScreen].scale);

//        UIColor *color = [UIColor colorWithRed:0 green:122/255 blue:255/255 alpha:0.4];
        UIColor *color = [UIColor colorWithRed:6 / 255 green:122 / 255 blue:181 / 255 alpha:0.6]; //UIColorFromRGB(0x067AB5)
        [color set];

        UIRectFill(CGRectMake(0, 0, imageSize.width, imageSize.height));
//
//    UIFont *font = [UIFont boldSystemFontOfSize:36];
//    NSLog(@"font ===== %@", font);
//    NSLog(@"lineHeight ===== %d", font.lineHeight);
//    NSLog(@"pointSize ===== %d", font.pointSize);
//    NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
//    [attributes setObject:font forKey:NSFontAttributeName];
//    [attributes setObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
//    CGFloat x = (width - font.pointSize) / 2;
//    CGFloat y = (width - font.lineHeight) / 2;
//
//
//    CGPoint point = CGPointMake(x, y);
//    NSLog(@"x ===== %f", x);


        UIImage *pressedColorImg = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        defaulImage = pressedColorImg;
    }

    return defaulImage;
}


- (UIImage *)getPersonImage:(NSString *)withName {
    CGFloat width = 50;
    CGSize imageSize = CGSizeMake(width, width);
    UIGraphicsBeginImageContextWithOptions(imageSize, 0, [UIScreen mainScreen].scale);
    [[XYQDrawPersonImage randomColor] set];
    UIRectFill(CGRectMake(0, 0, imageSize.width, imageSize.height));

    UIFont *font = [UIFont boldSystemFontOfSize:36];
    NSLog(@"font ===== %@", font);
    NSLog(@"lineHeight ===== %d", font.lineHeight);
    NSLog(@"pointSize ===== %d", font.pointSize);
    NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
    [attributes setObject:font forKey:NSFontAttributeName];
    [attributes setObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    CGFloat x = (width - font.pointSize) / 2;
    CGFloat y = (width - font.lineHeight) / 2;


    CGPoint point = CGPointMake(x, y);
    NSLog(@"x ===== %f", x);

    [[withName substringFromIndex:withName.length - 1] drawAtPoint:point withAttributes:attributes];


    UIImage *pressedColorImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return pressedColorImg;

}

- (UIImage *)imageWithStringWaterMark:(NSString *)markString inImage:(UIImage *)image atPoint:(CGPoint)point color:(UIColor *)color font:(UIFont *)font {
    CGFloat width = 120;
    CGSize w = CGSizeMake(width, width);
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0) {
        UIGraphicsBeginImageContextWithOptions(w, NO, 0.0); // 0.0 for scale means "scale for device's main screen".
    }
#else
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 4.0)
    {
        UIGraphicsBeginImageContext(w);
    }
#endif


    CGFloat lineHeight = font.lineHeight;
    CGFloat x = width / 2 - lineHeight;
    CGFloat y = width / 2 - lineHeight;

    NSLog(@"x: %d", x);
    NSLog(@"y: %d", y);
    NSLog(@"lineHeight: %d", lineHeight);




    //水印文字
//    [markString drawAtPoint:point withFont:font];
    NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
    [attributes setObject:font forKey:NSFontAttributeName];

    [markString drawAtPoint:point withAttributes:attributes];

    UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext();

    //文字颜色
    [color set];
    //原图
    [newPic drawInRect:CGRectMake(0.5, 0.5, width, width)];


    UIGraphicsEndImageContext();


    return newPic;
}

//- (UIImage *)getPersonImage:(NSString *)withName {
//
////    (NSString *)qrString size:(CGFloat)imageSize fillColor:(UIColor *)fillColor
//
//    CGFloat imageSize = 64;
//    UIColor *fillColor = [XYQDrawPersonImage randomColor];
//
////    CGFloat size = imageSize * [[UIScreen mainScreen] scale];
////
////
////    // create context
////    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
////
////    // The constants for specifying the alpha channel information are declared with the CGImageAlphaInfo type but can be passed to this parameter safely.
////
////    CGContextRef ctx = CGBitmapContextCreate(0, size, size, 8, size * 4, colorSpace, (CGBitmapInfo) kCGImageAlphaPremultipliedLast);
////
////    CGAffineTransform translateTransform = CGAffineTransformMakeTranslation(0, -size);
////    CGAffineTransform scaleTransform = CGAffineTransformMakeScale(1, -1);
////    CGContextConcatCTM(ctx, CGAffineTransformConcat(translateTransform, scaleTransform));
////
////
////    // get image
////    CGImageRef qrCGImage = CGBitmapContextCreateImage(ctx);
//    UIImage *image = [self imageWithStringWaterMark:[withName substringFromIndex:withName.length - 1] inImage:image atPoint:CGPointMake(0, 0) color:fillColor font:[UIFont boldSystemFontOfSize:24]];
//
//
//    // free memory
////    CGContextRelease(ctx);
////    CGImageRelease(qrCGImage);
////    CGColorSpaceRelease(colorSpace);
//
//    return image;
//}



@end